﻿using System;
using System.IO;
using System.Windows.Forms;
using Rsdn.Janus.Core.Extensibility.UI;
using Rsdn.Janus.ObjectModel;
using Rsdn.SmartApp;

namespace Rsdn.Janus
{
    /// <summary>
    /// Обработчик команд приложения.
    /// </summary>
    [CommandTarget]
    internal sealed class ApplicationCommandTarget : CommandTarget
    {
        public ApplicationCommandTarget(IServiceProvider serviceProvider)
            : base(serviceProvider)
        {
        }

        [CommandExecutor("Janus.Application.Exit")]                   //MLHIDE
        public void ExecuteExit(ICommandContext context)
        {
            Application.Exit();
        }

        [CommandExecutor("Janus.Application.About")]                  //MLHIDE
        public void ExecuteShowAbout(ICommandContext context)
        {
            using (AboutJanusForm aboutJanusForm = new AboutJanusForm(context))
            {
                IUIShell uiShell = context.GetRequiredService<IUIShell>();
                aboutJanusForm.ShowDialog(uiShell.GetMainWindowParent());
            }
        }

        [CommandExecutor("Janus.Application.Help")]                   //MLHIDE
        public void ExecuteShowHelp(ICommandContext context)
        {
            string helpFilePath = Path.Combine(EnvironmentHelper.GetJanusRootDir(), "JanusDoc.chm"); //MLHIDE

            if (File.Exists(helpFilePath))
            {
                Help.ShowHelp(new Control(), helpFilePath);
            }
            //ToDo: если файла нет, то нужно выдавать предупреждение
        }

        [CommandExecutor("Janus.Application.SendBugReport")]          //MLHIDE
        public void ExecuteSendBugReport(ICommandContext context)
        {
            IOutboxManager outboxManager = context.GetRequiredService<IOutboxManager>();
            outboxManager.AddBugReport(
                SR.MainForm.BugReport.DefaultName,
                SR.MainForm.BugReport.DefaultDescription,
                SR.MainForm.BugReport.DefaultStackTrace,
                true);
        }

        [CommandExecutor("Janus.Application.UserOptions")]            //MLHIDE
        public void ExecuteShowUserOptions(ICommandContext context)
        {
            string orgdbp = LocalUser.DatabasePath;

            IUIShell uiShell = context.GetRequiredService<IUIShell>();
            using (OptionsUserForm ouf = new OptionsUserForm(context, false))
            {
                if (ouf.ShowDialog(uiShell.GetMainWindowParent()) != DialogResult.OK)
                {
                    return;
                }

                if (orgdbp == LocalUser.DatabasePath)
                {
                    return;
                }

                //IFeature activeFeature = ObjectModel.Features.Instance.ActiveFeature;
                //if (activeFeature != null)
                //{
                //    activeFeature.Refresh();
                //}

                // Refresh all features not forums only
                IFeature[] features = Features.Instance.GetAllFeatures();
                foreach (IFeature feature in features)
                {
                    feature.Refresh();
                }

                context.GetRequiredService<IMainWindowService>().UpdateText();
            }
        }

        [CommandExecutor("Janus.Application.AppOptions")]             //MLHIDE
        public void ExecuteShowAppOptions(ICommandContext context)
        {
            bool oldValue = Config.Instance.ForumDisplayConfig.ShowUnreadThreadsOnly;
            using (OptionsForm of = new OptionsForm())
            {
                IWin32Window owner = context.GetRequiredService<IUIShell>().GetMainWindowParent();

                DialogResult res = of.ShowDialog(owner);

                if ((of.ActionType & ChangeActionType.Refresh) == ChangeActionType.Refresh
                        && res == DialogResult.OK)
                {
                    if (oldValue != Config.Instance.ForumDisplayConfig.ShowUnreadThreadsOnly)
                    {
                        Forums.Instance.Refresh();
                    }

                    Features.Instance.ConfigChanged();

                    if (Config.Instance.TickerConfig.ShowTicker)
                    {
                        Ticker.ShowTicker(context);
                    }
                    else
                    {
                        Ticker.HideTicker();
                    }

                    context.GetRequiredService<IMainWindowService>().Refresh();
                }

                if ((of.ActionType & ChangeActionType.Restart) == ChangeActionType.Restart
                    && res == DialogResult.OK)
                {
                    MessageBox.Show(
                        owner,
                        SR.MainForm.AppNeedRestart,
                        ApplicationInfo.ApplicationName,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
            }
        }

        [CommandExecutor("Janus.Application.ExtManagerForm")]             //MLHIDE
        public void ExecuteShowExtManager(ICommandContext context)
        {
            bool oldValue = Config.Instance.ForumDisplayConfig.ShowUnreadThreadsOnly;
            using (ExtensionManagerForm form = new ExtensionManagerForm(context))
            {
                IWin32Window owner = context.GetRequiredService<IUIShell>().GetMainWindowParent();

                DialogResult res = form.ShowDialog(owner);

                if (res == DialogResult.OK)
                {
                    MessageBox.Show(
                        owner,
                        SR.MainForm.AppNeedRestart,
                        ApplicationInfo.ApplicationName,
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
            }
        }

        [CommandExecutor("Janus.Application.ShowMainForm")]           //MLHIDE
        public void ExecuteRestoreMainForm(ICommandContext context)
        {
            context.GetRequiredService<IMainWindowService>().EnsureVisible();
        }

        [CommandStatusGetter("Janus.Application.ShowMainForm")]       //MLHIDE
        public CommandStatus QueryShowMainFormStatus(ICommandContext context)
        {
            IMainWindowService mainWindowService = context.GetService<IMainWindowService>();
            return mainWindowService != null
                       ? CommandStatus.Normal
                       : CommandStatus.Unavailable;
        }
    }
}